package com.ldy.core.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldy.core.entity.BaseLogs;
import com.ldy.core.entity.BaseUser;
import com.ldy.core.mapper.BaseUserMapper;
import com.ldy.core.service.BaseLogsService;
import com.ldy.core.service.BaseUserService;

import cn.hutool.core.collection.CollectionUtil;

@Service
public class BaseUserServiceImpl implements BaseUserService {

	@Autowired
	private BaseUserMapper mapper;

	@Autowired
	private BaseLogsService logsService;

	@Override
	public Page<BaseUser> selectPage(int current, int size, String username, String nickname) {
		Page<BaseUser> page = new Page<BaseUser>(current, size);
		page.setRecords(mapper.selectPageList(page, username, nickname));
		return page;
	}

	@Override
	public BaseUser selectByUsername(String username) {
		QueryWrapper<BaseUser> query = new QueryWrapper<>();
		query.eq("username", username);
		BaseUser cert = mapper.selectOne(query);
		return cert;
	}

	@Override
	public void saveOrUpdate(BaseUser user) {
		BaseUser old = this.selectByUsername(user.getUsername());
		Date now = new Date();
		if (null == old) {
			// 不存在，新增
			user.setLastUpdateTime(now);
			user.setCreateTime(now);
			this.mapper.insert(user);
			logsService.insert(BaseLogs.TYPE_CJ, "创建用户【" + user.getUsername() + "】");
		} else {
			user.setId(old.getId());
			user.setLastUpdateTime(now);
			this.mapper.updateById(user);
		}
	}

	@Override
	public BaseUser login(String username, String password) {
		Assert.hasText(username, "用户名不能为空");
		Assert.hasText(password, "密码不能为空");
		BaseUser user = this.selectByUsername(username);
		Assert.notNull(user, "用户名或密码错误");
		Assert.isTrue(password.equals(user.getPassword()), "用户名或密码错误");
		Assert.isTrue(user.getStatus() == BaseUser.Ok, "用户状态无效，请联系管理员");
		return user;
	}

	@Override
	public void register(BaseUser user) {
		Assert.notNull(user, "参数不能为空");
		Assert.hasText(user.getUsername(), "用户名不能为空");
		Assert.hasText(user.getPassword(), "密码不能为空");
//		Assert.hasText(user.getNickname(), "昵称不能为空");
		BaseUser exist = this.selectByUsername(user.getUsername());
		Assert.isNull(exist, "用户名已存在");
		Date now = new Date();
		user.setLastUpdateTime(now);
		user.setCreateTime(now);
		if (StringUtils.isEmpty(user.getRole())) {			
			user.setRole("user");
		}
		this.mapper.insert(user);
	}

	@Override
	public void updateUserStatus(String id) {
		BaseUser user = mapper.selectById(id);
		user.setStatus(user.getStatus() == BaseUser.Ok ? BaseUser.Disable : BaseUser.Ok);
		user.setLastUpdateTime(new Date());
		mapper.updateById(user);
	}


	@Override
	public void update(BaseUser user) {
		mapper.updateById(user);
	}

	@Override
	public BaseUser selectById(String userId) {
		return mapper.selectById(userId);
	}

	@Override
	public List<BaseUser> selectList(QueryWrapper<BaseUser> query) {
		return mapper.selectList(query);
	}

	@Override
	public BaseUser selectByPhone(String phone) {
		QueryWrapper<BaseUser> query = new QueryWrapper<>();
		query.eq("phone", phone);
		List<BaseUser> list = mapper.selectList(query);
		if (!CollectionUtil.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public void delete(String id) {
		mapper.deleteById(id);
	}

}
